<template>
  <div
    :class="className"
    :style="{height: height, width: width}"
  />
</template>

<script lang="ts">
import * as echarts from 'echarts'
import { EChartsOption, PieSeriesOption } from 'echarts'
import { Component, Prop } from 'vue-property-decorator'
import { mixins } from 'vue-class-component'
import ResizeMixin from '@/components/Charts/mixins/resize'

@Component({
  name: 'IssueCategoriesChart',
  components: {}
})
export default class extends mixins(ResizeMixin) {
  @Prop({ default: 'chart' }) private className!: string
  @Prop({ default: '100%' }) private width!: string
  @Prop({ default: '300px' }) private height!: string
  @Prop({ default: false }) private ring!: boolean

  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  }

  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  }

  private async initChart() {
    this.chart = echarts.init(this.$el as HTMLDivElement)

    const data = [{
      name: '重大问题数量',
      value: 23
    }, {
      name: '严重问题数量',
      value: 9
    }, {
      name: '一般问题数量',
      value: 4
    }
    ]
    this.setOptions(data)
  }

  private setOptions(data: any[]) {
    if (!this.chart) {
      return
    }

    // ...

    const option: EChartsOption =
      {
        legend: {
          bottom: 0
        },
        tooltip: {
          trigger: 'item'
        },
        series: [
          {
            type: 'pie',
            radius: this.ring ? ['40%', '75%'] : null,
            label: {
              // show: false,
              position: 'inside',
              formatter: '{c}'
            },
            data: data
          } as PieSeriesOption
        ]
      }

    console.log(JSON.stringify(option, null, 2))

    this.chart.setOption(option)
  }
}
</script>

<style lang="scss" scoped>

</style>
